package com.wjm.blog.common.util;

import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 字符串工具类
 * @Author: Ailos
 * @Date: 2020/4/20 10:39
 * @Version: 1.0
 */
public class StringUtil extends org.apache.commons.lang3.StringUtils {
    /**
     * @Description: 根据参数类型，判断是否非空
     * @Param: value
     * @Return: boolean
     * @Author: Ailos
     * @Date: 2020/4/21 10:26
     * @Version: 1.0
     */
    public static boolean isNotEmpty(Object value) {
        return !isEmpty(value);
    }

    /**
     * @Description: 根据参数类型，判断是否为空
     * @Param: value
     * @Return: boolean
     * @Author: Ailos
     * @Date: 2020/4/21 10:26
     * @Version: 1.0
     */
    public static boolean isEmpty(Object value) {
        if (value == null) {
            return true;
        }

        if (value instanceof String) {
            return ((String) value).length() == 0;
        }
        if (value instanceof Collection) {
            return ((Collection<? extends Object>) value).isEmpty();
        }
        if (value instanceof Map) {
            return ((Map<? extends Object, ? extends Object>) value).size() == 0;
        }
        if (value instanceof CharSequence) {
            return ((CharSequence) value).length() == 0;
        }

        // These types would flood the log
        // Number covers: BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
        if (value instanceof Boolean) {
            return false;
        }
        if (value instanceof Number) {
            return false;
        }
        if (value instanceof Character) {
            return false;
        }
        if (value instanceof java.util.Date) {
            return false;
        }
        return false;
    }

    //去空格、行
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 纯数字型
     */
    private static final Pattern digitalPattern = Pattern.compile("^(-?\\d+)(\\.\\d+)?$|[0-9]\\d*$");

    public static boolean isDigital(String str) {
        Matcher matcher = digitalPattern.matcher(str);
        return matcher.matches();
    }
}